package com.chixing.ih.module.task.controller;

import com.chixing.ih.json.ResponseData;

import com.chixing.ih.module.task.pojo.Comment;
import com.chixing.ih.module.task.pojo.DTO.MongoDBCommentDTO;
import com.chixing.ih.module.task.service.ICommentService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 目的:
 *
 * @author ：zcw
 * @version: 1.0  2020/10/3 23:05
 */
@RestController
@RequestMapping("comment")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "查询所有评论接口", notes = "查询所有评论")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("comments")
    public Map<String, Object> findAll() {
        List<Comment> list = commentService.findAll();
        return ResponseData.REP_GET_SUCCESS(list);
    }

    @ApiOperation(value = "根据父id查询评论接口", notes = "根据评论id查询评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentParentId", value = "评论父id", required = true, dataType = "Long"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("replyComment/{commentParentId}")
    public Map<String, Object> findByCommentParentId(@PathVariable("commentParentId") Long commentParentId) {
        List<MongoDBCommentDTO> list = commentService.findByCommentParentId(commentParentId);
        return ResponseData.REP_GET_SUCCESS(list);
    }

    @ApiOperation(value = "根据任务id查询评论接口", notes = "根据任务id查询评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "Long"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("firstLevelComments/{taskId}")
    public Map<String, Object> findByTaskId(@PathVariable("taskId") Long taskId) {
        List<MongoDBCommentDTO> list = commentService.findByTaskIdAndCommentParentId(taskId);
        return ResponseData.REP_GET_SUCCESS(list);
    }


    @ApiOperation(value = "添加评论接口", notes = "添加评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "content", value = "评论内容", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "评论者id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "commentParentId", value = "评论父id", required = true, dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "添加成功"),
            @ApiResponse(code = 400, message = "添加失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @PostMapping("comment")
    public Map<String, Object> saveComment(@RequestBody Comment comment) {
        commentService.saveComment(comment);
        return ResponseData.REP_CREATED_SUCCESS();
    }

    @ApiOperation(value = "更新评论接口", notes = "更新评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "commentId", value = "评论id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "content", value = "评论内容", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "评论者id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "commentParentId", value = "评论父id", required = true, dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "添加成功"),
            @ApiResponse(code = 400, message = "添加失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @PutMapping("comment")
    public Map<String, Object> updateById(@RequestBody Comment comment) {
        commentService.updateById(comment);
        return ResponseData.REP_ACCEPTED_SUCCESS();
    }

    @ApiOperation(value = "点赞接口", notes = "点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "评论id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "userId", value = "发布者id", required = true, dataType = "Long"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "添加成功"),
            @ApiResponse(code = 400, message = "添加失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @PutMapping("thumbUp")
    public Map<String, Object> thumbUp(@RequestBody Comment comment) {

        Object obj = redisTemplate.opsForValue().get("thumbUp_" + comment.getUserId() + "_" + comment.getCommentId());

        if (null == obj) {
            commentService.thumbUp(comment.getCommentId());
            redisTemplate.opsForValue().set("thumbUp_" + comment.getUserId() + "_" + comment.getCommentId(), 1);
            return ResponseData.REP_ACCEPTED_SUCCESS();
        }
        return ResponseData.REP_INVALID_ERROR();

    }

    @ApiOperation(value = "删除评论接口", notes = "删除评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId", value = "评论id", required = true, dataType = "Long")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "添加成功"),
            @ApiResponse(code = 400, message = "添加失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @DeleteMapping("comment/{commentId}")
    public Map<String, Object> deleteByCommentId(@PathVariable("commentId") Long commentId) {
        commentService.deleteByCommentId(commentId);
        return ResponseData.REP_DELETE_SUCCESS();
    }


}
